﻿package net.cooltain.warhammer.running.entity;

import java.util.List;

import net.cooltain.utils.Logger;
import net.cooltain.warhammer.framework.constant.ConstantPhases;
import net.cooltain.warhammer.framework.constant.ConstantPlayZone;

public class HandZone extends ZoneEntity{
	
	
	public HandZone(MotionPlayerZone player) {
		super(player);
		this.zoneConstant = ConstantPlayZone.HAND_ZONE;
	}

	/**
	 * 增加1张卡
	 * @param card
	 */
	public void addCard(MotionCard card){
		this.cards.add(card);
		card.currentZone = ConstantPlayZone.HAND_ZONE;
	}
	
	/**
	 * 增加X张卡
	 * @param card
	 */
	public void addCards(List<MotionCard> cards){
		for(MotionCard card : cards){
			card.currentZone = ConstantPlayZone.HAND_ZONE;
		}
	}
	
	/**
	 * 随机丢弃X张卡
	 * @param card
	 */
	public void randomThrowCard(int number){
		// TODO 
	}
	
	/**
	 * return 手牌列表
	 */
	public List<MotionCard> getHandCards(){
		return this.cards;
	}
	
	/**
	 * @return 手牌总数
	 */
	public int getCardSize(){
		return this.cards.size();
	}
	
	// 手牌所允许玩家激活的所有主动行为 ====START==================================
	
	/**
	 * 将卡片做为设施卡打出
	 * @param handIndex 在手中的序号
	 * @param targetZone 要放置的目标区域
	 */
	public boolean playCardAsDeveloper(int handIndex, Integer targetZone){
		if(!player.isMyTurn && !player.allowDeveloperOperateTurn){
			if(player.isLogger){
				Logger.warn(new StringBuffer()
					.append("现在是对方的回合,你不能放置设施卡")
				);
			}
			return false;
		}
		if( player.isMyTurn && !player.allowDeveloperMyTurn){
			if(player.isLogger){
				Logger.warn(new StringBuffer()
					.append("你不允许在本阶段中打出设施卡")
				);
			}
			return false;
		}
		
		Integer currentPhases = null;
		if(player.isMyTurn ){
			currentPhases = player.currentPhases;
		}else{
			currentPhases = player.operatePlayer.currentPhases;			
		}
		if( !player.allowDeveloperPhases.get(player.currentPhases)){
			if(player.isLogger){
				Logger.warn(new StringBuffer()
					.append("不允许在").append(ConstantPhases.allPhases.get(currentPhases)).append("打出设施卡")
				);
			}
			return false;
		}
		
		if( player.allowDeveloperSet<=0){
			if(player.isLogger){
				Logger.warn(new StringBuffer().append("你已经打出过设施卡,或者你不允许打出设施卡了"));
			}
			return false;
		}
		
		if( handIndex > cards.size()-1){
			if(player.isLogger){
				Logger.error(new StringBuffer().append("非法操作( Out of Index)"));
			}
			return false;
		}
		
		if(!ConstantPlayZone.developerAllowZone.containsKey(targetZone)){
			if(player.isLogger){
				Logger.warn(new StringBuffer()
					.append(player.playerName).append("的")
					.append(cards.get(handIndex).cardEntity.getName())
					.append("不能做为设施卡被放置到 ")
					.append(ConstantPlayZone.developerAllowZone.get(targetZone))
				);
			}
			return false;
		}
		
		// TODO 
		if(cards.get(handIndex).cardEntity.playAsDeveloper(player, cards.get(handIndex) ,targetZone) ){
			return true;
		}else{
			return false;
		}
	}
	// 手牌所允许玩家激活的所有主动行为 ==== END ==================================
	

    /**
     * 事件:增加一张手牌
     */
	@Override
	public void eventAddCard(MotionCard card) {
		super.eventAddCard(card);
		
		// 自己可以看到自己的手牌
		card.addVisiblePlayer(player);
	}
}
